/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.tnmk.worktime.gui.widget.text;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import javax.swing.JTextField;

/**
 *
 * @author Administrator
 */
public class JCustomTextField extends JTextField {
  private String placeHolder;
  @Override
  protected void paintComponent(java.awt.Graphics g) {
    super.paintComponent(g);
    if (placeHolder == null) placeHolder = "";
    String text = getText();
    if (text == null || text.isEmpty()) {
      Graphics2D g2 = (Graphics2D) g.create();
      g2.setColor(Color.gray);
      g2.setBackground(new Color(200,200,200));
      g2.setFont(getFont().deriveFont(Font.ITALIC));
      double height = this.getSize().getHeight();
      int fontSize = g2.getFont().getSize();
      int textY = ((int)height + (fontSize*2)/3)/2;
//      LogUtils.printCaller(this, "Height:"+height + ", fontSize:"+fontSize +", textY:"+textY, 1);
      g2.drawString(placeHolder, 0, textY); //figure out x, y from font's FontMetrics and size of component.
      g2.dispose();
    }
  }

  public String getPlaceHolder() {
    return placeHolder;
  }

  public void setPlaceHolder(String placeHolder) {
    this.placeHolder = placeHolder;
  }
}
